const form = document.querySelector('.registerbox')
const nameInp = document.querySelector('.username')
const pwdInp = document.querySelector('.password')
const rpwdInp = document.querySelector('.rpassword')
const nickInp = document.querySelector('#nickname')
const errBox = document.querySelector('.warning')
const checkbox= document.querySelector('#checkbox')
form.addEventListener('submit', e => {
  e.preventDefault()
  if(!checkbox.checked) return
  const info = {
    username: nameInp.value,
    password: pwdInp.value,
    rpassword: rpwdInp.value,
    nickname: nickInp.value
  }
  
  if (!info.username || !info.password || !info.rpassword || !info.nickname) return alert('请完整填写表单')
  if (info.password !== info.rpassword) return alert('两次密码不一样')

  ajax({
    url: 'http://localhost:8888/users/register',
    method: 'POST',
    data: info,
    dataType: 'json',
    success (res) {
      if (res.code === 0 ) {
        errBox.style.opacity = 1;

      } else {
        alert('注册成功, 点击确定跳转到登录页')
        window.location.href = './login.html'
      }
    }
  })
})
